//
//  TXPPlayView.m
//  StayAwhile
//
//  Created by dllo on 16/2/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPPlayView.h"
#import "UIImage+Blur.h"

@interface TXPPlayView ()

@property (nonatomic,assign)BOOL result;

@property (nonatomic,assign)BOOL isRotation;
@end
@implementation TXPPlayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self createSubView];
    }
    
    return self;
}
- (void)createSubView{
    
    self.myImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
    [self addSubview:self.myImageView];
    
    
    self.musicImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.myImageView.frame.origin.x+40, self.myImageView.frame.origin.y+50, self.myImageView.frame.size.width-80, 330)];
    self.musicImgView.userInteractionEnabled = YES;
    [self addSubview:self.musicImgView];
    self.musicImgView.layer.masksToBounds = YES;
    self.musicImgView.layer.borderWidth = 2;
    self.musicImgView.layer.cornerRadius = 160;
    self.musicImgView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(self.musicImgView.frame.origin.x+100,self.musicImgView.frame.origin.y+self.musicImgView.frame.size.height+25 , 200, 40)];
   
    // self.nameLab.font = [UIFont systemFontOfSize:15.0];
    [self addSubview:self.nameLab];
    
    self.sliderMusic = [[UISlider alloc]initWithFrame:CGRectMake(self.musicImgView.frame.origin.x, self.nameLab.frame.origin.y + 60, self.musicImgView.frame.size.width, 20)];
    self.sliderMusic.minimumTrackTintColor = [UIColor yellowColor];
    [self.sliderMusic setThumbImage:[UIImage imageNamed:@"slider_thum.png"] forState:UIControlStateNormal];
    [self addSubview:self.sliderMusic];
    
    self.downImg = [[UIImageView alloc] initWithFrame:CGRectMake(self.musicImgView.frame.origin.x + self.musicImgView.frame.size.width-30, self.nameLab.frame.origin.y + 40, 20, 20)];
    self.downImg.image = [UIImage imageNamed:@"iconfont-xiazai.png"];
    [self addSubview:self.downImg];
    
    self.playBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBnt.frame = CGRectMake(self.musicImgView.frame.origin.x, self.nameLab.frame.origin.y + 35, 20, 20);
    [self.playBnt setImage:[UIImage imageNamed:@"iconfont-shunxuxunhuan.png"] forState:UIControlStateNormal];
    [self addSubview:self.playBnt];
   
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake(self.sliderMusic.frame.origin.x + 270, self.sliderMusic.frame.origin.y + 33, 30, 30);
    [self.nextButton setImage:[UIImage imageNamed:@"iconfont-xiayiqu-3.png"] forState:UIControlStateNormal];
    [self addSubview:self.nextButton];
    
    self.upButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.upButton.frame = CGRectMake(self.sliderMusic.frame.origin.x+30, self.nextButton.frame.origin.y, 30, 30);
    [self.upButton setImage:[UIImage imageNamed:@"iconfont-shangyiqu-4.png"] forState:UIControlStateNormal];
    [self addSubview:self.upButton];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame = CGRectMake(self.sliderMusic.frame.origin.x+150, self.nextButton.frame.origin.y, 30, 30);
    [self.playButton setImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
    [self addSubview:self.playButton];
    
    self.timeLab = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.frame.origin.x, self.nameLab.frame.origin.y+self.nameLab.frame.size.height, 200, 30)];
    self.timeLab.text = @"0:0/0:0";
    self.timeLab.textColor = [UIColor redColor];
    [self addSubview:self.timeLab];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, self.nameLab.frame.origin.y + 150, self.myImageView.frame.size.width - 40, 4)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
  
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(250, 190, 284, 18)];
    [self.slider setMinimumTrackImage:nil forState:UIControlStateNormal];
    [self.slider setMinimumTrackImage:nil forState:UIControlStateNormal];
    [self.slider setThumbImage:nil forState:UIControlStateNormal];
    self.slider.transform =  CGAffineTransformMakeRotation(1.57079633);
    self.slider.value = 0;
    self.slider.minimumValue = 0.1;
    self.slider.maximumValue = 1.0;
    self.slider.alpha = 0;
    [self addSubview:self.slider];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.musicImgView addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.result==NO) {
        [UIView animateWithDuration:0.8 animations:^{
            
            self.slider.alpha = 1.0;
        }];
    }else{
        
        [UIView animateWithDuration:0.8 animations:^{
            
            self.slider.alpha = 0;
        }];
    }
    self.result = !self.result;
    
    
}

- (void)setModle:(TXPRadioModel *)modle
{
    
    _modle = modle;
    
    if (_modle != nil) {
        NSURL *url = [NSURL URLWithString:_modle.Info.imgUrl];
        [self.musicImgView sd_setImageWithURL:url placeholderImage:nil];
        // 改变View z轴值
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //旋转的起始值,从0点开始旋转
        animation.fromValue = [NSNumber numberWithInt:0];
        //最终旋转的角度是360度
        animation.toValue = [NSNumber numberWithInt:M_PI * 2];
        //旋转时长
        animation.duration = 10;
        //重复次数,NSIntegerMax为无限旋转
        animation.repeatCount = NSIntegerMax;
        //旋转结束后是否要逆向返回原位置
        animation.autoreverses = NO;
        //是否按照结束位置继续旋转
        animation.cumulative = YES;
        //给view的layer层添加动画,key是标记随便起名字
        [self.musicImgView.layer addAnimation:animation forKey:@"basicAnimation"];
        self.isRotation = NO;
        NSData *data = [NSData dataWithContentsOfURL:url];
         UIImage *image = [UIImage imageWithData:data];
        self.myImageView.image = [image boxblurImageWithBlur:0.8];
        self.nameLab.text = _modle.Info.title;
    }

}

- (void)stopImgeView{
    
    
    if (self.isRotation == NO) {
        //每一个view的layer层系统都设置了记录时间的属性,通过改变view动画时间来控制动画效果
        
        //获得到当前旋转的时间点
        CFTimeInterval stopTime = [self.musicImgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        //设置播放速度为0,即停止
        self.musicImgView.layer.speed = 0;
        //记录当前时间偏移量
        self.musicImgView.layer.timeOffset = stopTime;
        
    }else
    {
        //获得上次停止的时间偏移量
        CFTimeInterval stopTime = self.myImageView.layer.timeOffset;
        //设置速度1.0
        self.musicImgView.layer.speed = 1.0;
        //设置偏移量为0
        self.musicImgView.layer.timeOffset = 0;
        self.musicImgView.layer.beginTime = 0;
        //设置开始时间
        self.myImageView.layer.beginTime = [self.myImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
        
    }
    self.isRotation = !self.isRotation;

}
@end
